Skip to content

Fix int(QuadPrecision) for NaN, Inf, and out-of-int64 values#104

Merged
SwayamInSync merged 9 commits into
numpy:mainfrom
SwayamInSync:fix-int-conversion-97
Jun 5, 2026
Merged

Fix int(QuadPrecision) for NaN, Inf, and out-of-int64 values#104
SwayamInSync merged 9 commits into
numpy:mainfrom
SwayamInSync:fix-int-conversion-97

Conversation

@SwayamInSync
Copy link
Copy Markdown
Member

closes #97

AI Disclosure:
Claude wrote the test cases

@SwayamInSync SwayamInSync changed the title Fix int(QuadPrecision) for NaN, Inf, and out-of-int64 values (#97) Fix int(QuadPrecision) for NaN, Inf, and out-of-int64 values May 19, 2026
Comment thread src/csrc/scalar_ops.cpp Outdated
// Route the longdouble backend through quad as as_integer_ratio does;
// the prior `(long long)longdouble_value` cast also saturated/UBed on
// NaN/Inf/out-of-range.
value = Sleef_cast_from_doubleq1((double)self->value.longdouble_value);
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both this and the similar cast alluded to in the comment will lose precision on any platform with extended precision native long double.

Instead of doing this, you should write a utility function that checks for NaN and inf with std::isnan and std::isinf, which do support extended precision long double natively and then write the value to a string buffer, which you can parse however you need it.

Comment thread tests/test_quaddtype.py Outdated
assert int(QuadPrecision(str(n))) == n

@pytest.mark.parametrize("exponent", [40, 60, 80, 100])
def test_int_powers_of_two_far_above_int64(self, exponent):
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This and many other tests in this file aren't parametrized by the backend, which leads to missing the issue I pointed out above.

- name: Install Intel SDE
run: |
curl -o /tmp/sde.tar.xz https://downloadmirror.intel.com/859732/sde-external-9.58.0-2025-06-16-lin.tar.xz
SDE_URL="https://github.com/SwayamInSync/numpy-quaddtype/releases/download/sde-toolchain/sde-external-10.8.0-2026-03-15-lin.tar.xz"
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

URL isn't working for curl(same in NumPy repo), although interactively works.
Self-hosting from my fork, till some other solution comes in

@SwayamInSync
Copy link
Copy Markdown
Member Author

Cool, those were important catches. I fixed them all from complete codebase and its ready for 2nd review round.

Copy link
Copy Markdown
Member

@ngoldbaum ngoldbaum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I didn't go over the new test logic in detail but it looks like the issue I spotted in the last round is fixed.

Comment thread .github/workflows/test_old_cpu.yml Outdated
- name: Install Intel SDE
run: |
curl -fSL -o /tmp/sde.tar.xz https://github.com/nihui/ncnn-assets/releases/download/toolchain/sde-external-10.8.0-2026-03-15-lin.tar.xz
SDE_URL="https://downloadmirror.intel.com/859732/sde-external-9.58.0-2025-06-16-lin.tar.xz"
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a TODO to revert this when Intel's mirror is less flaky?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, I will keep this in check, right now the binary is hosted from my own fork's release assets, so no 3rd party here.

@SwayamInSync SwayamInSync merged commit 8b305c9 into numpy:main Jun 5, 2026
13 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] int(QuadPrecision('inf'|'nan'|'1e30')) silently returns INT64_MAX

2 participants